package top.wuyang.study.java24.servlet.cpuinfo;


import top.wuyang.study.java24.dao.TbCpuInfoDAO;
import top.wuyang.study.java24.entity.Result;
import top.wuyang.study.java24.entity.TbCpuInfo;
import top.wuyang.study.java24.util.JsonUtils;
import top.wuyang.study.java24.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;

@WebServlet(name = "cpuadd", urlPatterns = "/cpu/add.action")
public class AddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        resp.setContentType("application/json");
        try {
             // 获取请求参数,前端递交的参数
            String cpu = req.getParameter("cpu");
            String info = req.getParameter("info");
            Integer cores = Integer.parseInt(req.getParameter("cores"));
            Integer threads = Integer.parseInt(req.getParameter("threads"));
            String frequency = req.getParameter("frequency");
            String max_frequency = req.getParameter("max_frequency");
            Date post_time = StringUtil.parseDate(req.getParameter("post_time"));
            // 校验,参数不能为空
            if (StringUtil.isBlank(cpu)) {
                Result<String> result = Result.fail("cpu名称不能为空", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            if (StringUtil.isBlank(info)) {
                Result<String> result = Result.fail("cpu信息不能为空", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            if (StringUtil.isBlank(frequency)) {
                Result<String> result = Result.fail("cpu频率不能为空", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            if (StringUtil.isBlank(max_frequency)) {
                Result<String> result = Result.fail("cpu最大频率不能为空", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            TbCpuInfo checkcpu = TbCpuInfoDAO.queryByCpu(cpu);
            if (checkcpu!=null){
                Result<String> result = Result.fail("cpu已存在", null);
                writer.println(JsonUtils.stringify(result));
                return;
            }
            // 封装对象,添加信息
            TbCpuInfo tbCpuInfo = new TbCpuInfo();
            tbCpuInfo.setCpu(cpu);
            tbCpuInfo.setInfo(info);
            tbCpuInfo.setCores(cores);
            tbCpuInfo.setThreads(threads);
            tbCpuInfo.setFrequency(frequency);
            tbCpuInfo.setMax_frequency(max_frequency);
            tbCpuInfo.setPost_time(post_time);
            int i = TbCpuInfoDAO.insert(tbCpuInfo);
            if (i == 1) {
                Result<String> result = Result.success("添加成功", null);
                writer.println(JsonUtils.stringify(result));
            } else {
                Result<String> result = Result.fail("添加失败", null);
                writer.println(JsonUtils.stringify(result));
            }

        } catch (Exception e) {
            e.printStackTrace();
            Result<String> result = Result.fail("发生错误", null);
            writer.println(JsonUtils.stringify(result));
        }


    }
}
